package com.earthea.scaffold.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.earthea.scaffold.common.entity.BaseInsertableEntity;
import com.earthea.scaffold.common.enums.OperateTypeEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@TableName("t_oper_log")
public class OperLog extends BaseInsertableEntity {
    /**
     * 日志主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 操作模块
     */
    private String title;

    /**
     * 操作类型
     */
    private OperateTypeEnum operateType;

    /**
     * 请求方法
     */
    private String requestMethod;

    /**
     * 请求URL
     */
    private String requestUrl;

    /**
     * 请求参数
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL)
    private String request;

    /**
     * 返回参数
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL)
    private String response;

    /**
     * 异常信息
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL)
    private String errMsg;

    /**
     * 请求IP
     */
    private String ip;

    /**
     * 客户端操作系统
     */
    private String os;

    /**
     * 客户端浏览器
     */
    private String browser;

    /**
     * 操作状态（0正常 1异常）
     */
    private int status;

    /**
     * 消耗时间
     */
    private long costTime;

    public String requestLog(String loginUserName) {
        String template = "request start user [%s] operate [%s-%s] path [%s %s] request [%s]";
        return String.format(template, loginUserName, title, operateType.getLabel(), requestMethod, requestUrl, request);
    }

    public String responseLog(String loginUserName) {
        String template = "request end user [%s] operate [%s-%s] path [%s %s] response [%s]";
        return String.format(template, loginUserName, title, operateType.getLabel(), requestMethod, requestUrl, response);
    }

    public String errLog(String loginUserName) {
        String template = "request error user [%s] operate [%s-%s] path [%s %s]";
        return String.format(template, loginUserName, title, operateType.getLabel(), requestMethod, requestUrl);
    }
}
